package com.gibs.peixunapi.service.impl;

import com.gibs.peixunapi.VO.RoleVO;
import com.gibs.peixunapi.dao.UserAndRoleDao;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.model.User;
import com.gibs.peixunapi.model.UserAndRole;
import com.gibs.peixunapi.result.PageData;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.UserAndRoleService;
import com.gibs.peixunapi.utils.ConverterUtil;
import com.gibs.peixunapi.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class UserAndRoleServiceImpl implements UserAndRoleService{

	@Autowired
	private UserAndRoleDao userAndRoleDao;

	@Transactional
@Override
	public Result create(@Valid UserAndRole object) {
		try {
		
			userAndRoleDao.save(object);
			return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Transactional
@Override
	public Result save(@Valid UserAndRole object) {
		try {
			
			UserAndRole userAndRole = userAndRoleDao.getOne(object.getId());
			ConverterUtil.copyNotNullProperties(object,userAndRole);
			userAndRoleDao.save(userAndRole);
			return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result delete(Integer id) {
		try {
//			String[] ids = id.split(",");
//			for (String str : ids) {
//				userAndRoleDao.delete(Integer.valueOf(str));
//			}
			return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


@Override
	public Result  get(Integer id) {
		try {
			HashMap<String, Object> data = new HashMap<String, Object>();
			UserAndRole object = userAndRoleDao.getOne(id);
//			if (object == null) {
//				Result(false, Status.FAILD, "查找失败", null);
//			}
			HashMap<String,Object> map = new HashMap();
			map.put("id", object.getId());
			map.put("UserId", object.getUserId()==null?"":object.getUserId());
			map.put("RoleId", object.getRoleId()==null?"":object.getRoleId());
			data.put("object",map);

			return ResultUtil.success(ResultEnum.FIND_SUCCESS, data);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Override
	public Result  getList(String key) {
		try {
			List<UserAndRole> list = new ArrayList<>();
			if (!StringUtils.isEmpty(key)) {
//				list = userAndRoleDao.findLike("%" + key + "%");
			} else {
				list = userAndRoleDao.findAll();
			}
//			if (list.size() == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
			List<Object> data = new ArrayList<>();
			for(UserAndRole object : list){
				HashMap<String,Object> map = new HashMap();
				map.put("id", object.getId());
				map.put("UserId", object.getUserId()==null?"":object.getUserId());
				map.put("RoleId", object.getRoleId()==null?"":object.getRoleId());
				data.add(map);
			}
			return ResultUtil.success(ResultEnum.FIND_SUCCESS, data);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result getListPage(String key, Integer current, Integer limit) {
		try {
			List<UserAndRole> list = new ArrayList<>();
			limit = limit == null ? 10 : limit;
			current = current == null ? 1 : current;
			int offset = (current - 1) * limit;
			Long total = null;
			if (!StringUtils.isEmpty(key)) {
				list = userAndRoleDao.findPageLike("%" + key + "%", offset, limit);
				total = userAndRoleDao.countLike("%" + key + "%");
			} else {
				list = userAndRoleDao.findPage(offset, limit);
				total = userAndRoleDao.count();
			}
//			if (total == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
			List<Object> data = new ArrayList<>();
			for(UserAndRole object : list){
				HashMap<String,Object> map = new HashMap();
				map.put("id", object.getId());
				map.put("UserId", object.getUserId()==null?"":object.getUserId());
				map.put("RoleId", object.getRoleId()==null?"":object.getRoleId());
				data.add(map);
			}
			PageData pageData = new PageData(0, limit, current, data);
			return ResultUtil.successPage(ResultEnum.SUCCESS, pageData);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	

@Override
	public Result getOptions(String key) {
		try {
			List<UserAndRole> list = new ArrayList<>();
			if (!StringUtils.isEmpty(key)) {
//				list = userAndRoleDao.findLike("%" + key + "%");
			} else {
				list = userAndRoleDao.findAll();
			}
			List<Object> data = new ArrayList<>();
			for (UserAndRole object : list) {
				HashMap<String, Object> map = new HashMap();
				map.put("value", object.getId());
				map.put("name", object.getUserId());
				data.add(map);
			}
			return ResultUtil.success(ResultEnum.SUCCESS, data);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Override
	public List<RoleVO> getRoleVOListByUserId(Integer userId) {
		List<UserAndRole> userAndRoleList = userAndRoleDao.findUserAndRolesById(userId);
		List<RoleVO> roleVOList = new ArrayList<>();
		for (UserAndRole userAndRole : userAndRoleList) {
			RoleVO roleVO = new RoleVO();
			BeanUtils.copyProperties(userAndRole.getRoleId(),roleVO);
			roleVOList.add(roleVO);
		}
		return roleVOList;
	}

	@Override
	public List<Integer> getUserIdListByRoleIdList(List<Integer> roleIdList) {
		return userAndRoleDao.findUserIdListById(roleIdList);
	}
}

